package com.ledim.ledimview;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.ledim.activity.EpisodesActivity;
import com.ledim.adapter.EpisodePagerAdapter;
import com.ledim.adapter.VarietyEpisodesAdapter;
import com.ledim.bean.LedimAlbumBean;
import com.ledim.bean.LedimEpisodeBean;
import com.ledim.bean.SubmediaEpisodesResponse;
import com.ledim.service.LedimApiServiceUtils;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.ConstantUtil;
import com.ledim.widget.view.IXListViewListener;
import com.ledim.widget.view.XListView;
import com.letv.android.young.client.R;
import java.util.ArrayList;

public class VideoAlbumsView extends LinearLayout
        implements View.OnClickListener, IXListViewListener {

    private static final int VARITYSHOW = 1;
    private static final int POSITIVE = 180001;
    private static final int TV = 181031;
    public static final int LAUNCH_MODE_ALBUM = 2;
    public static final int LAUNCH_MODE_TV = 5;
    public static final int LAUNCH_MODE_VARITY = 11;
    public static final int LAUNCH_MODE_RECORD = 16;
    public static final int LAUNCH_MODE_SELF = 1008;
    private Context mContext;
    private View mCloseAlbums;
    private View mScrollTabLayout;
    private HorizontalScrollView mScroll;
    private LinearLayout mTabView;
    private XListView mVarietyListview;
    private TextView mTitle;
    private TextView mChooseAll;
    private int mEpisodes;
    public VarietyEpisodesAdapter mImageAdapter;
    private int mTabWidth = 0;
    private int mCurrentPosition = 0;
    private boolean mBottom = true;
    private boolean mAddPlay;
    private LedimAlbumBean mAlbum;
    private String mMediaId;
    private String mSubMediaId;
    private ViewPager mViewPager;
    private EpisodePagerAdapter mPagerAdapter;
    private ArrayList<TextView> mTextViewList = new ArrayList<TextView>();
    private ArrayList<String> mEpisodesTab = new ArrayList<String>();
    private ArrayList<LedimEpisodeBean> subEpisodes = new ArrayList<LedimEpisodeBean>();
    public ArrayList<LedimEpisodeBean> mAllEpisodes = new ArrayList<LedimEpisodeBean>();
    private EpisodesActivity.EpisodesInterface mEpisodesInterface;
    private ArrayList<ViewPagerEpisodeListView> mViewPagerList =
            new ArrayList<ViewPagerEpisodeListView>();

    public VideoAlbumsView(Context context) {
        super(context);
        mContext = context;
    }

    public VideoAlbumsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public VideoAlbumsView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //        init();
    }

    //绑定数据
    public void bindData(LedimAlbumBean album, int mEpisodes, String mediaId, String subMediaId,
                         boolean mAddPlay, EpisodesActivity.EpisodesInterface episodesInterface) {
        this.mAlbum = album;
        this.mEpisodes = mEpisodes;
        this.mAddPlay = mAddPlay;
        mMediaId = mediaId;
        mSubMediaId = subMediaId;
        mEpisodesInterface = episodesInterface;
        init();
    }

    /**
     * 初始化界面
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void init() {
        mVarietyListview = (XListView) findViewById(R.id.listview_variety);
        mViewPager = (ViewPager) findViewById(R.id.episodes_viewpager);
        mCloseAlbums = findViewById(R.id.album_back_image);
        mScrollTabLayout = findViewById(R.id.channels_tabs_layout);
        mTabView = (LinearLayout) findViewById(R.id.tabs_view);
        mScroll = (HorizontalScrollView) findViewById(R.id.tabs_scroll);
        mTitle = (TextView) findViewById(R.id.episodes_tab_title);
        mChooseAll = (TextView) findViewById(R.id.episodes_add_all);

        mScrollTabLayout.setVisibility(View.GONE);
        mVarietyListview.setVisibility(View.GONE);
        mViewPager.setVisibility(View.GONE);
        mCloseAlbums.setOnClickListener(this);

        if (mAddPlay) {
            mVarietyListview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            mChooseAll.setVisibility(View.VISIBLE);
            mChooseAll.setOnClickListener(this);
            mTitle.setText("添加放映单");
        } else {
            mVarietyListview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            showTitleName();
        }
        mVarietyListview.setXListViewListener(this, 0);
        mVarietyListview.setPullRefreshEnable(true);
        mVarietyListview.setAdapter(null);

        if (mEpisodes == -1) {
            //显示图片文字
            mScrollTabLayout.setVisibility(View.GONE);
            mVarietyListview.setVisibility(View.VISIBLE);
            mViewPager.setVisibility(View.GONE);
            mVarietyListview.startHeaderRefresh();
        } else {
            //显示数字
            mScrollTabLayout.setVisibility(View.VISIBLE);
            mVarietyListview.setVisibility(View.GONE);
            mViewPager.setVisibility(View.VISIBLE);
            double tabCount = mEpisodes / 50.0;
            if (tabCount > (int) (tabCount)) {//>
                int i = 0;
                for (; i < (int) (tabCount); i++) {
                    mEpisodesTab.add(50 * i + 1 + " - " + 50 * (i + 1));
                }
                mEpisodesTab.add(50 * i + 1 + " - " + mEpisodes);
                initTabView(((int) (tabCount) + 1));
            } else {//=
                for (int i = 0; i < (int) (tabCount); i++) {
                    mEpisodesTab.add(50 * i + 1 + " - " + 50 * (i + 1));
                }
                initTabView((int) (tabCount));
            }
        }

    }

    private void initTabView(int dex) {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        if (dex >= 5) {
            mTabWidth = dm.widthPixels / 5;
        } else if (dex == 0) {
            mTabWidth = dm.widthPixels / 1;
        } else {
            mTabWidth = dm.widthPixels / dex;
        }
        mTabView.removeAllViews();
        mTextViewList.clear();

        if (dex != 0) {
            for (int i = 0; i < dex; i++) {
                mTabView.addView(addTabView(mEpisodesTab.get(i), i));
            }
        } else {
            mTabView.addView(addTabView(mEpisodesTab.get(0), 0));
        }

        mTextViewList.get(0).setTextColor(getResources().getColor(R.color.text_color_level8));
        mCurrentPosition = 0;
        initViewPager();
    }

    private void initViewPager() {
        mViewPagerList.clear();
        mPagerAdapter = null;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        for (int i = 0; i < mEpisodesTab.size(); i++) {
            ViewPagerEpisodeListView view =
                    (ViewPagerEpisodeListView) inflater.inflate(R.layout.tab_viewpager_episodes, null);
            mViewPagerList.add(view);
        }

        mViewPagerList.get(0)
                      .bindData(mAddPlay, mMediaId, mSubMediaId, mEpisodes, 0, true, mEpisodesInterface, mTabView);
        mViewPager.setVisibility(View.VISIBLE);

        if (mPagerAdapter == null) {
            mPagerAdapter = new EpisodePagerAdapter(mContext, mViewPagerList);
            mViewPager.setAdapter(mPagerAdapter);
        } else {
            mPagerAdapter.pList = mViewPagerList;
            mPagerAdapter.notifyDataSetChanged();
        }

        mViewPager.setCurrentItem(0);
        mScroll.smoothScrollTo(mTabWidth * (-2), 0);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int j = 0; j < mTextViewList.size(); j++) {
                    TextView textView = mTextViewList.get(j);
                    textView.setTextColor(getResources().getColor(R.color.text_color_level3));

                }
                mTextViewList.get(position)
                             .setTextColor(getResources().getColor(R.color.text_color_level8));
                mViewPagerList.get(position).setVisibility(View.VISIBLE);
                mScroll.smoothScrollTo(mTabWidth * (position - 2), 0);
                mCurrentPosition = position;

                if (!mViewPagerList.get(position).mIsReload) {
                    mViewPagerList.get(position)
                                  .bindData(mAddPlay, mMediaId, mSubMediaId, mEpisodes, position, true, mEpisodesInterface, mTabView);
                }
                //只是改变按钮颜色，控制剧集tab切换时
                int arg1 = 0;
                int arg2 = 0;
                if (mViewPagerList.get(position).mFigureAdapter != null) {
                    if (mViewPagerList.get(position).mFigureAdapter.pSelectedEpisodes.size() ==
                            mViewPagerList.get(position).mAllTVEpisodesList.size()) {
                        arg1 = -1;
                        arg2 = mViewPagerList.get(position).mFigureAdapter.pSelectedEpisodes.size();
                    } else {
                        arg1 = mViewPagerList.get(position).mFigureAdapter.pSelectedEpisodes.size();
                    }
                }
                mEpisodesInterface.selectEpisodesPage(arg1, arg2);

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    private View addTabView(String name, final int position) {
        View view = View.inflate(mContext, R.layout.tab_item1, null);
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.tab_item_layout);
        TextView textView = (TextView) view.findViewById(R.id.tab_item_name);
        textView.setText(name);
        mTextViewList.add(position, textView);

        layout.setOnClickListener(new OnClickListener() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
            @Override
            public void onClick(View v) {
                for (int j = 0; j < mTextViewList.size(); j++) {
                    TextView textView = mTextViewList.get(j);
                    textView.setTextColor(getResources().getColor(R.color.text_color_level3));
                }
                mTextViewList.get(position)
                             .setTextColor(getResources().getColor(R.color.text_color_level8));
                mViewPager.setCurrentItem(position);
                mCurrentPosition = position;
            }
        });
        return view;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.album_back_image:
                ((Activity) mContext).finish();
                break;
            case R.id.episodes_add_all:
                if (mEpisodes == -1) {
                    choseAllVarieties();
                } else {
                    if (mViewPagerList.get(mCurrentPosition).mAllTVEpisodesList.size() > 0) {
                        choseAllTVs();
                    }
                }
                break;
        }
    }

    @Override
    public void onRefresh(int id) {
        if (mEpisodes == -1) {
            //期加载
            //            mMediasTypeMediaSub_mediaEpisodesGetModel.getMediaLists(this, mMediaId,mSubMediaId,20,false,descType,false, 1);

            //            switch (mAlbum.varietyShow) {
            //                case VARITYSHOW://栏目1
            //                    mMediasTypeMediaSub_mediaEpisodesGetModel.getMediaLists(this, mMediaId, mSubMediaId, 20, false, 0, false, 2);
            //                    break;
            //                default:
            if (mAddPlay) {//添加播放单
                if (mAlbum.videoType != null && !mAlbum.videoType.equals("null") &&
                        !mAlbum.videoType.equals("")) {
                    switch (Integer.parseInt(mAlbum.videoType)) {
                        case POSITIVE://正片
                        case TV://TV版
                            //电影，电视剧，动漫,纪录片
                            if (mAlbum.getCategory() == ConstantUtil.ChannelType.film ||
                                    mAlbum.getCategory() == ConstantUtil.ChannelType.tv ||
                                    mAlbum.getCategory() == ConstantUtil.ChannelType.cartoon ||
                                    mAlbum.getCategory() == ConstantUtil.ChannelType.record ||
                                    mAlbum.getCategory() == ConstantUtil.ChannelType.self) {
                                LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 0, 2, 1, mediaListObserver);
                            } else if (mAlbum.getCategory() ==
                                    ConstantUtil.ChannelType.variety) {//综艺
                                LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 2, 1, mediaListObserver);
                            } else {//其它
                                LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, 1, mediaListObserver);
                            }
                            break;
                        default:
                            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, 1, mediaListObserver);
                            break;
                    }
                } else {
                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, 1, mediaListObserver);
                }

            } else {
                if (mAlbum.videoType != null && !mAlbum.albumType.equals("null")) {
                    switch (mAlbum.getalbumType()) {
                        case POSITIVE://正片
                        case TV://TV版
                            switch (mAlbum.getCategory()) {
                                case LAUNCH_MODE_ALBUM://电视剧、动漫2,5    得到升序剧集列表
                                case LAUNCH_MODE_TV:
                                case LAUNCH_MODE_RECORD://记录片16
                                case VARITYSHOW://电影1
                                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 0, 2, 1, mediaListObserver);
                                    break;
                                case LAUNCH_MODE_VARITY://综艺11    得到降序列表
                                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 2, 1, mediaListObserver);
                                    break;
                                default://音乐其它
                                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, 1, mediaListObserver);
                                    break;
                            }
                            break;
                        default:
                            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, 1, mediaListObserver);
                            break;
                    }
                } else {
                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, 1, mediaListObserver);
                }
            }
            //                    break;
            //            }
        }
    }

    private int mCurrentPage = 0;

    @Override
    public void onLoadMore(int id) {
        if (mEpisodes == -1) {
            //期加载
            //            mMediasTypeMediaSub_mediaEpisodesGetModel.getMoreMediaLists(this, mMediaId,mSubMediaId,20, false,descType);

            //            switch (mAlbum.varietyShow) {
            //                case VARITYSHOW://栏目1
            //                    mMediasTypeMediaSub_mediaEpisodesGetModel.getMoreMediaLists(this, mMediaId, mSubMediaId, 20, false, 0, 2);
            //                    break;
            //                default:
            if (mAddPlay) {//添加播放单
                if (!mAlbum.videoType.equals("null") && !mAlbum.videoType.equals("")) {
                    switch (Integer.parseInt(mAlbum.videoType)) {
                        case POSITIVE://正片
                        case TV://TV版
                            //电影，电视剧，动漫
                            if (mAlbum.getCategory() == ConstantUtil.ChannelType.film ||
                                    mAlbum.getCategory() == ConstantUtil.ChannelType.tv ||
                                    mAlbum.getCategory() == ConstantUtil.ChannelType.cartoon ||
                                    mAlbum.getCategory() == ConstantUtil.ChannelType.record ||
                                    mAlbum.getCategory() == ConstantUtil.ChannelType.self) {
                                LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 0, 2, ++mCurrentPage, mediaListObserver);
                            } else if (mAlbum.getCategory() ==
                                    ConstantUtil.ChannelType.variety) {//综艺
                                LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 2, ++mCurrentPage, mediaListObserver);
                            } else {//纪录片等其它
                                LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, ++mCurrentPage, mediaListObserver);
                            }
                            break;
                        default:
                            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, ++mCurrentPage, mediaListObserver);
                            break;
                    }
                } else {
                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, ++mCurrentPage, mediaListObserver);
                }
            } else {
                if (!mAlbum.albumType.equals("null")) {
                    switch (mAlbum.getalbumType()) {
                        case POSITIVE://正片
                        case TV://TV版
                            switch (mAlbum.getCategory()) {
                                case LAUNCH_MODE_ALBUM://电视剧、动漫2,5    得到升序剧集列表
                                case LAUNCH_MODE_TV:
                                case LAUNCH_MODE_RECORD://记录片16
                                case VARITYSHOW://电影1
                                case LAUNCH_MODE_SELF://自制1008
                                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 0, 2, ++mCurrentPage, mediaListObserver);
                                    break;
                                case LAUNCH_MODE_VARITY://综艺11    得到降序列表
                                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 2, ++mCurrentPage, mediaListObserver);
                                    break;
                                default:
                                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, ++mCurrentPage, mediaListObserver);
                                    break;
                            }
                            break;
                        default:
                            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, ++mCurrentPage, mediaListObserver);
                            break;
                    }
                } else {
                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 20, 1, 1, ++mCurrentPage, mediaListObserver);
                }
            }
            //                    break;
            //            }
        }
    }

    /**
     * 显示图文剧集
     */
    private void showEpisodesListView(ArrayList<LedimEpisodeBean> episodes) {
        subEpisodes.clear();
        mImageAdapter = null;
        for (int i = 0; i < episodes.size(); i++) {
            subEpisodes.add(episodes.get(i));
        }
        mImageAdapter =
                new VarietyEpisodesAdapter(mContext, subEpisodes, mAddPlay, mEpisodesInterface);
        mVarietyListview.setAdapter(mImageAdapter);

        for (int i = 0; i < episodes.size(); i++) {
            if (episodes.get(i).id == Integer.valueOf(mSubMediaId)) {
                mImageAdapter.setSelectItem(i);
                if (mBottom) {
                    //listview显示指定位置
                    if (i == 0) {
                        mVarietyListview.setSelection(i);
                    } else {
                        mVarietyListview.setSelection(i + 1);
                    }
                } else {
                    //加载更多时,不总置顶,显示加载更多之前一页的
                    mVarietyListview.setSelection(episodes.size() - 21);//20是每页数据条数
                }
            }
        }
        mImageAdapter.notifyDataSetChanged();
    }

    //显示视频标题
    private void showTitleName() {
        if (mAlbum != null) {
            switch (mAlbum.varietyShow) {
                case VARITYSHOW://栏目
                    mTitle.setText("专辑列表");
                    break;
                default: // 0--非栏目
                    if (mAddPlay) {//添加播放单
                        if (mAlbum.videoType != null && !mAlbum.videoType.equals("null") &&
                                !mAlbum.videoType.equals("")) {
                            switch (Integer.parseInt(mAlbum.videoType)) {
                                case POSITIVE://正片
                                case TV://TV版
                                    if (mAlbum.getCategory() == ConstantUtil.ChannelType.film) {
                                        mTitle.setText("正片");
                                    } else if (
                                            mAlbum.getCategory() == ConstantUtil.ChannelType.tv ||
                                                    mAlbum.getCategory() ==
                                                            ConstantUtil.ChannelType.cartoon) {
                                        mTitle.setText("剧集");
                                    } else if (mAlbum.getCategory() ==
                                            ConstantUtil.ChannelType.variety) {//综艺
                                        mTitle.setText("往期列表");
                                    } else {//纪录片等其它
                                        mTitle.setText("相关视频");
                                    }
                                    break;
                                default:
                                    mTitle.setText("相关视频");
                                    break;
                            }
                        } else {
                            mTitle.setText("相关视频");
                        }
                    } else {
                        if (mAlbum.albumType != null && !mAlbum.albumType.equals("null")) {
                            switch (mAlbum.getalbumType()) {
                                case POSITIVE://正片
                                case TV://TV版
                                    if (mAlbum.getCategory() == ConstantUtil.ChannelType.film) {//电影
                                        mTitle.setText("正片");
                                    } else if (
                                            mAlbum.getCategory() == ConstantUtil.ChannelType.tv ||
                                                    mAlbum.getCategory() ==
                                                            ConstantUtil.ChannelType.cartoon) {//电视剧
                                        mTitle.setText("剧集");
                                    } else if (mAlbum.getCategory() ==
                                            ConstantUtil.ChannelType.variety) {//综艺
                                        mTitle.setText("往期列表");
                                    } else {//纪录片等其它
                                        mTitle.setText("相关视频");
                                    }
                                    break;
                                default:
                                    mTitle.setText("相关视频");
                                    break;
                            }
                        } else {
                            //null--娱乐
                            mTitle.setText("相关视频");
                        }
                    }

                    break;
            }
        } else {
            mTitle.setText("相关视频");
        }

    }

    //处理电视剧类的全选
    private void choseAllTVs() {
        int arg1 = 0, arg2;

        if (mChooseAll.getText().equals("全选")) {
            mViewPagerList.get(mCurrentPosition).mFigureAdapter.pSelectedEpisodes.clear();
            mViewPagerList.get(mCurrentPosition).mFigureAdapter.pSelectionMap.clear();
            mChooseAll.setText("取消");
            if (mViewPagerList.get(mCurrentPosition).mAllTVEpisodesList.size() > 0) {
                for (int i = 0; i < mViewPagerList.get(mCurrentPosition).mAllTVEpisodesList.size();
                     i++) {
                    mViewPagerList.get(mCurrentPosition).mFigureAdapter.pSelectionMap.put(i, true);
                }
                mViewPagerList.get(mCurrentPosition).mFigureAdapter.pSelectedEpisodes.addAll(mViewPagerList
                        .get(mCurrentPosition).mAllTVEpisodesList);
                arg1 = 1;
            }
            mViewPagerList.get(mCurrentPosition).mFigureAdapter.notifyDataSetChanged();

        } else if (mChooseAll.getText().equals("取消")) {
            mChooseAll.setText("全选");
            if (mViewPagerList.get(mCurrentPosition).mAllTVEpisodesList.size() > 0) {
                for (int i = 0; i < mViewPagerList.get(mCurrentPosition).mAllTVEpisodesList.size();
                     i++) {
                    mViewPagerList.get(mCurrentPosition).mFigureAdapter.pSelectionMap.put(i, false);
                }
                arg1 = -1;
                mViewPagerList.get(mCurrentPosition).mFigureAdapter.pSelectedEpisodes.clear();
            }
            mViewPagerList.get(mCurrentPosition).mFigureAdapter.notifyDataSetChanged();
        }
        //控制全选按钮，只要没有全选就显示全选
        if (mViewPagerList.get(mCurrentPosition).mFigureAdapter.pSelectedEpisodes.size() ==
                mViewPagerList.get(mCurrentPosition).mFigureAdapter.pList.size()) {
            arg2 = 1;
        } else {
            arg2 = -1;
        }
        mEpisodesInterface.selectAllEpisodes(arg1, arg2, mViewPagerList.get(mCurrentPosition).mFigureAdapter.pSelectedEpisodes);
    }

    //处理综艺类的全选
    private void choseAllVarieties() {
        int arg1 = 0, arg2;
        if (mChooseAll.getText().equals("全选")) {
            mImageAdapter.pSelectedEpisodes.clear();
            mImageAdapter.pSelectionMap.clear();
            mChooseAll.setText("取消");
            if (mAllEpisodes.size() > 0) {
                for (int i = 0; i < mAllEpisodes.size(); i++) {
                    mImageAdapter.pSelectionMap.put(i, true);
                }
                arg1 = 1;
                mImageAdapter.pSelectedEpisodes.addAll(mAllEpisodes);
            }
            mImageAdapter.notifyDataSetChanged();

        } else if (mChooseAll.getText().equals("取消")) {
            mChooseAll.setText("全选");
            if (mAllEpisodes.size() > 0) {
                for (int i = 0; i < mAllEpisodes.size(); i++) {
                    mImageAdapter.pSelectionMap.put(i, false);
                }
                arg1 = -1;
                mImageAdapter.pSelectedEpisodes.clear();
            }
            mImageAdapter.notifyDataSetChanged();
        }
        //控制全选按钮，只要没有全选就显示全选
        if (mImageAdapter.pSelectedEpisodes.size() == mImageAdapter.getDatas().size()) {
            arg2 = 1;
        } else {
            arg2 = -1;
        }
        mEpisodesInterface.selectAllEpisodes(arg1, arg2, mImageAdapter.pSelectedEpisodes);
    }

    private LedimHttpObserver mediaListObserver =
            new LedimHttpObserver<SubmediaEpisodesResponse>() {
                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    mVarietyListview.setVisibility(GONE);
                }

                @Override
                public void onNext(SubmediaEpisodesResponse response) {
                    super.onNext(response);
                    if (response != null && response.success) {
                        mVarietyListview.setVisibility(VISIBLE);
                        mVarietyListview.stopRefresh();
                        mVarietyListview.stopLoadMore();
                        if (response.ext.totalNum <= mCurrentPage * 20) {
                            mCurrentPage = 1;
                        }
                        if (mEpisodes == -1) {
                            //期
                            mAllEpisodes.clear();
                            mAllEpisodes.addAll(response.data);
                            showEpisodesListView(response.data);
                            //加载更多
                            if (response.paged.more) {
                                mVarietyListview.setPullLoadEnable(true);
                                mBottom = false;
                            } else {
                                mVarietyListview.setPullLoadEnable(false);
                            }
                        }

                    }
                }

            };
}
